package utils

import (
	_ "embed"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"math/rand"
	"net/http"
	"net/rpc"
	"push_course/src/data"
	"push_course/src/entity"
	"strings"
	"time"
)


// 加载课表信息
var CourseList_ff = LoadJson(data.COURSE_JSON_FF)
var CourseList_yy = LoadJson(data.COURSE_JSON_YY)


// 通过 val 找 key
func FindValByKey(obj map[string]string, val string) string {
	for k, v := range obj {
		if v == val {
			return k
		}
	}
	return ""
}

// 随机一言
func GetRandomSentences() string {

	var categoryMap = make(map[string]string)

	categoryMap["网易云"] = "j"
	categoryMap["文学"] = "d"
	categoryMap["哲学"] = "k"

	category := categoryMap["哲学"]
	url := fmt.Sprintf("https://v1.hitokoto.cn/?c=%s&encode=text", category)
	resp, err := http.Get(url)
	if err != nil {
		log.Println(err)
	}
	defer resp.Body.Close()
	text, _ := ioutil.ReadAll(resp.Body)
	log.Print(string(text))
	return fmt.Sprintf(`<strong>%s</strong><p style="text-align:right;">自-%s</p>`, string(text), FindValByKey(categoryMap, category))
}






// rpc推送
func PushMsgToWechatRpc( req entity.Req, res *entity.Res) {
	client, err := rpc.DialHTTP("tcp", data.RPC_ADDR)
	if err != nil {
		log.Println(err)
	}
	defer client.Close()
	client.Call("PushServer.PushToWechat", req, &res)
}

func PushMsgToWechatGroupRpc(req entity.Req, res *entity.Res)  {
	client, err := rpc.DialHTTP("tcp", data.RPC_ADDR)
	if err != nil {
		log.Println(err)
	}
	defer client.Close()
	client.Call("PushServer.PushToWechatGroup", req, &res)
}


type Item struct {
	A string `json:"1"`
	B string `json:"2"`
	C string `json:"3"`
	D string `json:"4"`
	E string `json:"5"`
}

func LoadJson(jsonStr string) []Item {
	type Items struct {
		List []Item `json:"data"`
	}

	items := Items{}

	err := json.Unmarshal([]byte(jsonStr), &items)
	if err != nil {
		log.Println(err)

	}

	return items.List
}

// 获取城市天气
func GetCityWeather(city string) (string, string) {
	resp, err := http.Get(fmt.Sprintf("http://wthrcdn.etouch.cn/weather_mini?city=%v", city))
	if err != nil {
		log.Println(err)
	}
	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)

	weatherBody := &struct {
		Data struct {
			Ganmao   string `json:"ganmao"`
			Forecast []struct {
				Date      string `json:"date"`
				High      string `json:"high"`
				Fengli    string `json:"fengli"`
				Low       string `json:"low"`
				Fengxiang string `json:"fengxiang"`
				Type      string `json:"type"`
			} `json:"forecast"`
		} `json:"data"`
	}{}

	err = json.Unmarshal([]byte(body), weatherBody)
	if err != nil {
		log.Println(err)
	}
	nowWeather := weatherBody.Data.Forecast[0]

	// default
	str := fmt.Sprintf("%v今日天气:%v,%v,%v,%v", city, nowWeather.Type, nowWeather.High, nowWeather.Low, nowWeather.Fengxiang)

	return str, weatherBody.Data.Ganmao
}

// 随机获取单词

//go:embed res/cet4.txt
var cet4word string

// 随机四级单词
func GetRandomWord() (string, string) {

	rand.Seed(time.Now().UnixNano())
	split := strings.Split(cet4word, "\n")
	num := len(split)
	var lineNum int
	func() {
		for true {
			lineNum = rand.Intn(num)
			if lineNum%2 != 0 && lineNum < num {
				lineNum--
				break
			}
		}

	}()
	return split[lineNum], split[lineNum+1]
}



// 获取剩余时间
func getRest() string {
	parse, _ := time.Parse("2006-01-02 15:04:05", "2021-10-30 09:00:00")
	// 过时了
	if time.Now().Unix() > parse.Unix() {
		return ""
	}
	// 否则计算倒计时
	hours := parse.Sub(time.Now()).Hours()
	days := int(hours / 24)
	str := fmt.Sprintf(`<h4>教资考试还剩:<span style="color:red;">%v天</span></h4>`, days)

	return str
}

